<template>
  <div class="dialog-overlay" @click="handleClose">
    <div class="dialog" @click.stop>
      <div class="dialog-header">
        <h2>{{ isEdit ? '编辑令牌' : '添加令牌' }}</h2>
        <button class="close-btn" @click="handleClose">×</button>
      </div>
      
      <form @submit.prevent="handleSubmit" class="token-form">
        <div class="form-group">
          <label class="form-label">服务名称 *</label>
          <input
            v-model="formData.serviceName"
            type="text"
            class="form-input"
            placeholder="例如：GitHub, Google, AWS"
            required
          />
        </div>
        
        <div class="form-group">
          <label class="form-label">账户名称</label>
          <input
            v-model="formData.accountName"
            type="text"
            class="form-input"
            placeholder="例如：user@example.com"
          />
        </div>
        
        <div class="form-group">
          <label class="form-label">密钥 * (TOTP Secret)</label>
          <input
            v-model="formData.secret"
            type="text"
            class="form-input"
            :placeholder="isEdit ? '留空表示不修改' : '例如：JBSWY3DPEHPK3PXP'"
            :required="!isEdit"
          />
          <small class="form-hint">
            从authenticator应用的设置中获取密钥，或使用otpauth://链接
          </small>
        </div>
        
        <div class="form-group">
          <label class="form-label">分类</label>
          <input
            v-model="formData.category"
            type="text"
            class="form-input"
            placeholder="例如：工作、个人、金融"
          />
        </div>
        
        <div class="form-group">
          <label class="form-label">备注说明</label>
          <textarea
            v-model="formData.notes"
            class="form-textarea"
            rows="4"
            maxlength="1000"
            placeholder="添加关于此令牌的说明...&#10;例如：&#10;- 工作账号，用于项目A和B&#10;- 每月1号需要更换密码&#10;- 紧急联系：张三 ext-1234"
          ></textarea>
          <small class="form-hint">
            {{ formData.notes?.length || 0 }} / 1000 字符
          </small>
        </div>
        
        <div class="form-row">
          <div class="form-group">
            <label class="form-label">算法</label>
            <select v-model="formData.algorithm" class="form-select">
              <option value="SHA1">SHA1</option>
              <option value="SHA256">SHA256</option>
              <option value="SHA512">SHA512</option>
            </select>
          </div>
          
          <div class="form-group">
            <label class="form-label">位数</label>
            <select v-model.number="formData.digits" class="form-select">
              <option :value="6">6位</option>
              <option :value="8">8位</option>
            </select>
          </div>
          
          <div class="form-group">
            <label class="form-label">周期（秒）</label>
            <select v-model.number="formData.period" class="form-select">
              <option :value="30">30秒</option>
              <option :value="60">60秒</option>
            </select>
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">您的密码 *</label>
          <input
            v-model="formData.password"
            type="password"
            class="form-input"
            placeholder="输入您的账户密码以加密令牌"
            required
          />
          <small class="form-hint">
            密码用于加密令牌密钥，确保安全性
          </small>
        </div>
        
        <div class="form-actions">
          <button type="button" @click="handleClose" class="btn btn-secondary">
            取消
          </button>
          <button type="submit" class="btn btn-primary" :disabled="loading">
            {{ loading ? '处理中...' : (isEdit ? '保存' : '添加') }}
          </button>
        </div>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';

const props = defineProps({
  token: {
    type: Object,
    default: null,
  },
});

const emit = defineEmits(['close', 'submit']);

const isEdit = ref(!!props.token);
const loading = ref(false);
const error = ref('');

const formData = reactive({
  serviceName: '',
  accountName: '',
  secret: '',
  category: '',
  notes: '',
  algorithm: 'SHA1',
  digits: 6,
  period: 30,
  password: '',
});

// 如果是编辑模式，填充表单数据
watch(
  () => props.token,
  (token) => {
    if (token) {
      formData.serviceName = token.serviceName || '';
      formData.accountName = token.accountName || '';
      formData.category = token.category || '';
      formData.notes = token.notes || '';
      formData.algorithm = token.algorithm || 'SHA1';
      formData.digits = token.digits || 6;
      formData.period = token.period || 30;
      formData.secret = ''; // 不显示现有密钥
      formData.password = ''; // 重新输入密码
    }
  },
  { immediate: true }
);

function handleClose() {
  emit('close');
}

async function handleSubmit() {
  error.value = '';
  loading.value = true;
  
  try {
    // 构建提交数据
    const submitData = { ...formData };
    
    // 如果是编辑模式且没有输入新密钥，删除secret字段
    if (isEdit.value && !submitData.secret) {
      delete submitData.secret;
    }
    
    emit('submit', submitData);
  } catch (err) {
    error.value = err.message || '操作失败，请重试';
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.dialog {
  background: var(--bg-color, #fff);
  border-radius: 16px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 24px 24px 16px;
  border-bottom: 1px solid var(--border-color, #e5e7eb);
}

.dialog-header h2 {
  margin: 0;
  font-size: 24px;
  color: var(--text-color, #333);
}

.close-btn {
  background: none;
  border: none;
  font-size: 32px;
  color: var(--text-secondary, #666);
  cursor: pointer;
  padding: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: all 0.2s;
}

.close-btn:hover {
  background: var(--bg-secondary, #f3f4f6);
  color: var(--text-color, #333);
}

.token-form {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--text-color, #333);
  font-size: 14px;
}

.form-input,
.form-select,
.form-textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-color, #d1d5db);
  border-radius: 8px;
  font-size: 15px;
  color: var(--text-color, #333);
  background: var(--bg-color, #fff);
  transition: all 0.2s;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
  outline: none;
  border-color: var(--primary-color, #3b82f6);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-textarea {
  resize: vertical;
  font-family: inherit;
  line-height: 1.5;
}

.form-hint {
  display: block;
  margin-top: 6px;
  font-size: 13px;
  color: var(--text-secondary, #6b7280);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--border-color, #e5e7eb);
}

.btn {
  padding: 10px 24px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--primary-color, #3b82f6);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark, #2563eb);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-secondary, #f3f4f6);
  color: var(--text-color, #333);
}

.btn-secondary:hover {
  background: var(--border-color, #e5e7eb);
}

.error-message {
  margin-top: 16px;
  padding: 12px;
  background: #fee;
  border: 1px solid #fcc;
  border-radius: 8px;
  color: #c33;
  font-size: 14px;
}

/* 深色模式支持 */
.dark-theme .dialog {
  background: #1f2937;
}

.dark-theme .dialog-header {
  border-bottom-color: #374151;
}

.dark-theme .form-input,
.dark-theme .form-select,
.dark-theme .form-textarea {
  background: #111827;
  border-color: #374151;
  color: #e5e7eb;
}

.dark-theme .close-btn:hover {
  background: #374151;
}

.dark-theme .btn-secondary {
  background: #374151;
  color: #e5e7eb;
}

.dark-theme .btn-secondary:hover {
  background: #4b5563;
}

/* 响应式 */
@media (max-width: 640px) {
  .dialog {
    width: 95%;
    max-height: 95vh;
  }
  
  .form-row {
    grid-template-columns: 1fr;
  }
  
  .dialog-header h2 {
    font-size: 20px;
  }
}
</style>

